package com.saris.primego.web.controller.admin;

import com.saris.primego.common.constant.JwtClaimsConstant;
import com.saris.primego.common.properties.JwtProperties;
import com.saris.primego.common.result.PageResult;
import com.saris.primego.common.result.Result;
import com.saris.primego.common.utils.JwtUtil;
import com.saris.primego.pojo.dto.EmployeeDTO;
import com.saris.primego.pojo.dto.EmployeeLoginDTO;
import com.saris.primego.pojo.dto.EmployeePageQueryDTO;
import com.saris.primego.pojo.entity.Employee;
import com.saris.primego.pojo.vo.EmployeeLoginVO;
import com.saris.primego.service.EmployeeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController("adminEmployeeController")
@RequestMapping("/admin/employee")
@Slf4j
@Tag(name = "员工管理")
public class EmployeeController {

    private final EmployeeService employeeService;
    private final JwtProperties jwtProperties;

    public EmployeeController(EmployeeService employeeService, JwtProperties jwtProperties) {
        this.employeeService = employeeService;
        this.jwtProperties = jwtProperties;
    }

    @Operation(summary = "用户登录", method = "POST")
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    @Operation(summary = "退出账号", method = "POST")
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }


    @PostMapping()
    @Operation(summary = "新增员工", method = "POST")
    public Result<Long> add(@RequestBody EmployeeDTO employeeDTO) {

        log.info("当前线程的Id：{}", Thread.currentThread().threadId());

        Long eId = this.employeeService.addEmployee(employeeDTO);
        if (eId != null)
            return Result.success(eId);
        else return Result.error("添加员工错误");
    }

    @GetMapping("/page")
    @Operation(summary = "查询员工", method = "GET")
    public Result<PageResult<Employee>> query(EmployeePageQueryDTO employeePageQueryDTO) {

        log.info("员工分页查询参数为：{}", employeePageQueryDTO);

        PageResult<Employee> list = employeeService.pagedQuery(employeePageQueryDTO);
        return Result.success(list);
    }

    @PostMapping("/status/{status}")
    @Operation(summary = "启闭用户", method = "POST")
    public Result<Void> startOrStop(@PathVariable Integer status, @RequestParam Long id) {
        log.info("需要开启/禁用的员工Id：{}", id);
        employeeService.startOrStop(status, id);
        return Result.success();
    }

    @GetMapping("/{id}")
    @Operation(summary = "按ID查询", method = "GET")
    public Result<Employee> getById(@PathVariable Long id) {
        log.info("查询员工的Id：{}", id);
        Employee employee = employeeService.getById(id);
        employee.setPassword("****");
        return Result.success(employee);
    }

    @PutMapping
    @Operation(summary = "编辑员工", method = "PUT")
    public Result<Void> update(@RequestBody EmployeeDTO employeeDTO) {
        log.info("修改员工的Id：{}", employeeDTO.getId());
        employeeService.update(employeeDTO);
        return Result.success();
    }

}
